<?php
require_once '../library/class.phpmailer.php';

set_time_limit(0);
date_default_timezone_set('Etc/GMT-8'); 

$link = mysql_connect('localhost', 'root', '111111') or die("Could not connect: " . mysql_error());
mysql_select_db("zhaoren");
mysql_query('SET NAMES utf8');

$upload_base_url = "http://renxun.net/?act=upload_search_info&em=";

$email_config['mail_host']	= 'mail.renxun.net';			// SMTP 服务器  
$email_config['send_user']	= 'admin@renxun.net';		// 用户名 
$email_config['send_pass']	= 'qwe123';				// 密码
$email_config['fromName']	= 'admin';				// 发信人别名
$email_config['frommail']	= 'admin@renxun.net';		// 发信人 

$sql = "SELECT * FROM collect_search_info WHERE send_tag = 0 LIMIT 0, 2000";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
	$id			= $row['id'];
	$tomail		= $row['email'];
//$tomail		= 'wangnannew@gmail.com';	
	$subject	= $row['subject'];
	$body		= $row['notice'];
	$upload_url	= $upload_base_url.$row['email_md5'];

	$mail_contents = file_get_contents('contents.html');
	
	$str			= array('$subject', '$body', '$upload_url');
	$replace_str	= array($subject, $body, $upload_url);	
	$mail_contents	= str_replace($str, $replace_str, $mail_contents);
	
//echo $mail_contents;exit;
	
	//发送成功
	if(send_email($tomail, $subject, $mail_contents) == 1)
	{		
		$send_date	= date('Y-m-d H:i:s', time());	
		$succ_sql	= "UPDATE collect_search_info SET send_tag = 1, send_date = '".$send_date."' WHERE id = $id";
		mysql_query($succ_sql);
		
		echo 1;
	}
	else
	{
		//发送失败
		echo 0;
	}

	sleep(2);
}


//邮件发送  
function send_email($tomail, $subject, $body) 
{	
	global $email_config;
	
	$mail_host	= $email_config['mail_host'];	// SMTP 服务器  
	$send_user	= $email_config['send_user'];	// 用户名 
	$send_pass	= $email_config['send_pass'];	// 密码
	$fromName	= $email_config['fromName'];	// 发信人别名
	$frommail	= $email_config['frommail'];	// 发信人  

	//$subject	= '恭喜'.$applicant_username.'报名成功';
	//$body		= "报名成功报名成功报名成功报名成功报名成功";
	$ccmail		='';
	
	$mail = new PHPMailer();  

	//$body	= $mail->getFile('contents.html');//邮件正文内容，提取html文件为其内容   
	//$body	= eregi_replace("[\]",'',$body);   
	
	$mail->IsSMTP();					// 经smtp发送  
	$mail->Host     = $mail_host;       // SMTP 服务器  
	$mail->SMTPAuth = true;             // 打开SMTP 认证  
	$mail->Username = $send_user;    	// 用户名  
	$mail->Password = $send_pass;       // 密码  
	$mail->From     = $frommail;        // 发信人  
	$mail->FromName = $fromName;        // 发信人别名  
	$mail->AddAddress($tomail);         // 收信人  
	if(!empty($ccmail)){  
		$mail->AddCC($ccmail);          // cc收信人  
	}  

	$mail->WordWrap = 50;  
	$mail->CharSet = 'UTF-8'; 
	//$mail->Encoding = 'base64';
	$mail->IsHTML(true);                // 以html方式发送

	$mail->Subject  = $subject;         // 邮件标题
	$mail->Body     = $body;            // 邮件内容  
	//$mail->AltBody  =  "请使用HTML方式查看邮件。";  
	
	return $mail->Send();  
}  

?>